import axios from "axios";

import { Message } from 'element-ui';
import store from "@/store";

const baseURL = "/api";

const request = (obj) => {

    const promise = axios({
        baseURL,
        // 统一添加头信息
        headers: {
            Authorization: store.state.loginUser.token
        },
        ...obj});
    
    promise.then(res => {
        // 逻辑失败：把后端返回的提示信息打印
        if (res.data.code < 0) {
            Message({
                message: res.data.msg,
                type: "error"
            });
        }
    },(error) => {
        console.log("error = ",error);
        Message({
            message: "发生网络错误",
            type: "error"
        });
    });

    return promise;
};

export default request;